ShiwenListLoader=function(){
	this.inited=false;
	this.appVM=null;
	this.options={};
	this.loadedShiwenIdList=[];
}

ShiwenListLoader.defValue={
	appVMConfig:{
		el:"#shiwen-list-app",
		data:{
			shiwenList:[],
		}
	},
	itemContentEl:null,
	itemEl:".mui-table-view-cell",
	itemTapFunc:function(loader,tappedEle){
		loader.toDetailPage(tappedEle);
	},
	loadParam:{
		pageNo:1,
		pageSize:20,
	},
	loadOverCallback:function(){}
}
//
ShiwenListLoader.prototype.init=function(opts){
	var that=this;
	if(!this.inited){
		this.options=$.extend(true,{},ShiwenListLoader.defValue,opts);
		this.appVM=new Vue(this.options.appVMConfig);
		if(this.options.itemContentEl && this.options.itemTapFunc){
			$(this.options.itemContentEl).on('tap',this.options.itemEl,function(){
				//第二个参数为当前tap的元素
				that.options.itemTapFunc(that,this);
			});
		}
	}
	this.inited=true;
}
//
ShiwenListLoader.prototype.getLoadParam=function(){
	return this.options.loadParam;
}
//
ShiwenListLoader.prototype.getOptions=function(){
	return this.options;
}
//
ShiwenListLoader.prototype.loadShiwenList=function(){
	var that=this;
	var appVMConfigData=this.options.appVMConfig.data;
	var url=getShiciServiceHost()+'/shici/gushiwen/list';
	muiAjaxGetJsonData({
		url:url,
		data:that.options.loadParam,
		callback:function(data){
			if(data.entry){
				if(data.entry.dataList && data.entry.dataList.length>0){
					mui.each(data.entry.dataList,function(index,itemInfo){
					   itemInfo.shortContent=itemInfo.shortContent.trim();
					   appVMConfigData.shiwenList.push(itemInfo);
					   that.loadedShiwenIdList.push(itemInfo.id);
					});
				}
				that.options.loadOverCallback(that,data);
			}
		},
	});
}
ShiwenListLoader.prototype.loadNextBigPageShiwenIdList=function(newIdList,callback){
	var that=this;
	var appVMConfigData=this.options.appVMConfig.data;
	var bigPageSize=500;
	var bigPageLoadParam=$.extend(true,{},that.options.loadParam,
									{
										pageSize:bigPageSize
									});
	var url=getShiciServiceHost()+'/shici/gushiwen/idList';
	muiAjaxGetJsonData({
		url:url,
		data:bigPageLoadParam,
		callback:function(data){
			if(data.entry){
				if(data.entry.dataList && data.entry.dataList.length>0){
					mui.each(data.entry.dataList,function(index,itemInfoId){
					   newIdList.push(itemInfoId);
					});
				}
			}
			callback();
		},
	});
}
//
ShiwenListLoader.prototype.emptyShiwenList=function(){
	var appVMConfigData=this.options.appVMConfig.data;
	appVMConfigData.shiwenList = [];
	this.loadedShiwenIdList=[];
}
//
ShiwenListLoader.prototype.toDetailPage=function(tappedEle){
	var index=$(tappedEle).attr('index');
	var dataList=this.options.appVMConfig.data.shiwenList;
	var indexData=dataList[index];
	var infoId=indexData.id;
	var infoTitle=indexData.title;
	var newIdList=[];
	mui.each(this.loadedShiwenIdList,function(index,itemInfoId){
	   newIdList.push(itemInfoId);
	});
	//
	console.info(infoId+":"+infoTitle);
	var callback=function(){
		var timestamp=new Date().getTime();
		mui.openWindow({
			url:'gushiwenDetail.html?infoId='+infoId+'&timestamp='+timestamp,
			id:'gushiwenDetail.html-infoId-'+infoId+'-'+timestamp,
			extras:{
				infoId : infoId,
				shiwenIdList : newIdList
			}
		});
	}
	if(this.options.loadParam.idList){
		callback();
	}else{
		this.loadNextBigPageShiwenIdList(newIdList,callback);
	}
}